package cn.com.nou.base.service.bdm;

import cn.com.nou.base.dao.bdm.SchoolSystemMapper;
import cn.com.nou.common.NouException;
import cn.com.nou.common.builder.PaginationBuilder;
import cn.com.nou.common.entity.NouEnumerationT;
import cn.com.nou.common.entity.Pagination;
import cn.com.nou.common.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class SchoolSystemService {
    @Autowired
    SchoolSystemMapper schoolSystemMapper;

    public Pagination getSchoolSystemlist(NouEnumerationT nouEnumerationT,int pageNo, int pageSize) {
        PaginationBuilder builder = new PaginationBuilder();
        Pagination page = builder.getPaginating(pageNo, pageSize);
        List<NouEnumerationT> schoolSystemList = schoolSystemMapper.getSchoolSystemList(nouEnumerationT,page.getOffset(), page.getLimit());
        page.setResultList(schoolSystemList);
        page.setTotal(schoolSystemMapper.getSchoolSystemCount(nouEnumerationT.getType()));
        page.setPageSize(pageSize);
        page.setPageNo(pageNo);
        page.setOffset(page.getOffset());
        page.setLimit(page.getLimit());
        return page;
    }

    /**
     * 新增学制
     */
    public int addSchoolSystem(NouEnumerationT nouEnumerationT)  {
        if (schoolSystemMapper.getSameItemName(nouEnumerationT).size() == 0) {
            nouEnumerationT.setId(Utils.getUUID());
            return schoolSystemMapper.addSchoolSystem(nouEnumerationT);
        } else {
            return 2;
        }
    }

    /**
     * 编辑学制
     * @param nouEnumerationT
     */
    public int updateSchoolSystem(NouEnumerationT nouEnumerationT) {
        if (schoolSystemMapper.getSameItemName(nouEnumerationT).size() == 0) {
            return schoolSystemMapper.updateSchoolSystem(nouEnumerationT);
        } else {
            return 2;
        }
    }

    /**
     * 删除学制
     * @param nouEnumerationT
     * @return
     */
    public List<Integer> deleteSchoolSystem(NouEnumerationT nouEnumerationT) {
        List<Integer> code = new ArrayList<>();
        NouEnumerationT nouEnumerationT1 = new NouEnumerationT();
        nouEnumerationT1.setId(nouEnumerationT.getId());
        nouEnumerationT1.setStatus("0");
        if (schoolSystemMapper.getStudentUsedCount(nouEnumerationT1) > 0) {
            code.add(1);
        }
        if (schoolSystemMapper.getScholUsedCount(nouEnumerationT) > 0) {
            code.add(2);
        }
        if (code.size() == 0 && schoolSystemMapper.updateSchoolSystem(nouEnumerationT) > 0) {
            code.add(3);
        }
        return code;
    }
}
